package com.liao.healthydiet;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.liao.healthydiet.domain.About;

public class ListViewFoodAboutLayoutAdapter extends BaseAdapter {

    private List<About> abouts;

    private Context context;
    private LayoutInflater layoutInflater;

    public ListViewFoodAboutLayoutAdapter(Context context, ArrayList<About> abouts) {
        this.context = context;
        this.abouts = abouts;
        this.layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return abouts.size();
    }

    @Override
    public About getItem(int position) {
        return abouts.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.listview_foodabout_layout, null);
            convertView.setTag(new ViewHolder(convertView));
        }
        initializeViews((About) getItem(position), (ViewHolder) convertView.getTag());
        return convertView;
    }

    private void initializeViews(About about, ViewHolder holder) {
        holder.imgIcon.setImageResource(about.getIcon());
        holder.tvInfo.setText(about.getInfo());
    }

    protected static class ViewHolder {
        private ImageView imgIcon;
        private TextView tvInfo;

        ViewHolder(View view) {
            imgIcon = (ImageView) view.findViewById(R.id.img_icon);
            tvInfo = (TextView) view.findViewById(R.id.tv_info);
        }
    }
}
